"""
Case Type   : resource pool
Case Name   : 验证创建资源池指定控制组区分大小写，部分测试点合理报错
Create At   : 2022/08/29
Owner       : songjing20
Description :
    1.预置两个子class控制组,大小写,1个Workload控制组
    2.创建资源池指定控制组class_a,CLASS_a
    3.创建资源池指定控制组Class_a
    4.创建资源池指定控制组workload_a1,大小写
    5.创建资源池指定timeshare控制组，大小写
    6.查看资源池关联的控制组
    7.环境清理
Expect      :
    1.创建成功
    2.成功
    3.区分大小写，该组不存在，合理报错
    4.区分大小写，大写的组不存在，合理报错
    5.小写报错
    6.5个
    7.清理成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ResourcePool(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.class_name1 = 'class_a'
        self.class_name2 = 'CLASS_a'
        self.group_name = 'workload_a1'
        self.respool = [f'resp_resource_pool_0023_{i}' for i in range(1, 10)]

    def test_resource_pool(self):
        text = "-----step1:预置子class控制组及Workload控制组;expect:创建成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -c -S {self.class_name1} -s 20;" \
              f"gs_cgroup -c -S {self.class_name2} -s 20;" \
              f"gs_cgroup -c -S {self.class_name1} -G {self.group_name} -g 20" \
              f"; gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertIn(f"CLASS  Name: {self.class_name1}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"CLASS  Name: {self.class_name2}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_name}", cmd_res,
                      f'执行失败:{text}')

        text = "-----step2:创建资源池指定控制组class_a,CLASS_a;expect:创建成功-----"
        self.log.info(text)
        sql = f"create resource pool {self.respool[0]} " \
              f"with(control_group = '{self.class_name1}');" \
              f"create resource pool {self.respool[1]} " \
              f"with(control_group = '{self.class_name2}');"
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertEqual(2, sql_res.count(
            self.constant.create_resource_pool_success), f'执行失败:{text}')

        text = "-----step3:创建资源池指定控制组Class_a;expect:报错-----"
        self.log.info(text)
        sql = f"create resource pool {self.respool[2]} " \
              f"with(control_group = 'Class_a');"
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn('ERROR:  invalid control group: Class_a',
                      sql_res, f'执行失败:{text}')

        text = "-----step4:创建资源池指定控制组workload_a1,大小写;expect:大写的组不存在，合理报错-----"
        self.log.info(text)
        sql = f"create resource pool {self.respool[3]} " \
              f"with(control_group = '{self.class_name1}:WORKload_a1');" \
              f"create resource pool {self.respool[4]} " \
              f"with(control_group = '{self.class_name1}:{self.group_name}');"
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(self.constant.create_resource_pool_success, sql_res,
                      f'执行失败:{text}')
        self.assertIn(f'ERROR:  invalid control group: {self.class_name1}:'
                      f'WORKload_a1', sql_res, f"执行失败:{text}")

        text = "-----step5:创建资源池指定timeshare控制组，大小写;expect:小写报错-----"
        self.log.info(text)
        sql = f"""create resource pool {self.respool[5]} 
        with(control_group = '{self.class_name1}:Low');
        create resource pool {self.respool[6]} 
        with (control_group = '{self.class_name1}:low');
        create resource pool {self.respool[7]} with(control_group='Medium');
        create resource pool {self.respool[8]} with(control_group='medium');"""
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(f"ERROR:  invalid control group: {self.class_name1}:low",
                      sql_res, f'执行失败:{text}')
        self.assertIn("ERROR:  invalid control group: medium", sql_res,
                      f'执行失败:{text}')
        self.assertEqual(2, sql_res.count(
            self.constant.create_resource_pool_success), f'执行失败:{text}')

        text = "-----step6:查询资源池关联的控制组;expect:5个资源池-----"
        self.log.info(text)
        sql = f"select respool_name, control_group from pg_resource_pool " \
              f"where respool_name like '{self.respool[0][:-1]}%' order by 1;"
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(
            f'{self.respool[0]} | {self.class_name1}\n '
            f'{self.respool[1]} | {self.class_name2}\n '
            f'{self.respool[4]} | {self.class_name1}:{self.group_name}\n '
            f'{self.respool[5]} | {self.class_name1}:Low\n '
            f'{self.respool[7]} | DefaultClass:Medium\n'
            f'(5 rows)', sql_res, f'执行失败:{text}')

    def tearDown(self):
        text = "-----step7:环境清理;expect:成功-----"
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            ';\n'.join(
                [f"drop resource pool if exists {i}" for i in self.respool]))
        self.log.info(drop_res)
        cmd = f"source {macro.DB_ENV_PATH};gs_cgroup --revert"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.assertEqual(9, drop_res.count(
            self.constant.drop_resource_pool_success), f'执行失败:{text}')
        self.assertEqual('', cmd_res, f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
